export default function filterAsyncRoutes(A, B) {
  const BMap = {};
  function buildBMap(node) {
    const key = node.path + node.name;
    BMap[key] = node;
    if (node.children) {
      node.children.forEach(child => buildBMap(child));
    }
  }
  B.forEach(item => buildBMap(item));

  function filterNode(nodeA) {
    const newNode = { ...nodeA };
    if (newNode.children) {
      newNode.children = newNode.children
        .filter(child => {
          const key = child.path + child.name;
          const childInB = BMap[key];
          return !!childInB;
        })
        .map(child => filterNode(child));
      if (newNode.children.length === 0) {
        delete newNode.children;
      }
    }
    return BMap[newNode.path + newNode.name] ? newNode : null;
  }

  return A.map(item => filterNode(item)).filter(node => node !== null);
}
